package com.abss.vote.service.impl;

import com.abss.vote.mapper.*;
import com.abss.vote.model.*;
import com.abss.vote.service.RedisService;
import com.abss.vote.service.UserBackstageService;
import com.abss.vote.util.FileUtils;
import com.abss.vote.util.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserBackstageServiceImpl implements UserBackstageService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private DistrictListMapper districtListMapper;
    @Autowired
    private PrizeMapper prizeMapper;
    @Autowired
    private GroupRuleMapper groupRuleMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private AdministratorsMapper administratorsMapper;
    @Autowired
    private AdminDeleteMapper adminDeleteMapper;
    @Autowired
    private MpInFoMapper mpInFoMapper;
    @Autowired
    private LogoMapper logoMapper;
    @Autowired
    private VotesMapper votesMapper;
    @Autowired
    RedisService redisService = new RedisServiceImpl();

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int getParticipantsCount(int ACTIVITY_ID,String isSup,int DISTRICT_ID,boolean flag) {
        int res= 0;
        //根据管理员的地区获取
        if(isSup.equals("0") && !flag){//普通管理员     获取该活动下本地区
            res = userMapper.getParticipantsCounts(ACTIVITY_ID,DISTRICT_ID);
        }else {//超级管理员      获取该活动下全部的
            res = userMapper.getParticipantsCount(ACTIVITY_ID);

        }
        return res;
    }

    @Override
    public int getWorksCount(int ACTIVITY_ID,String isSup,int DISTRICT_ID,boolean flag) {
        int res= 0;
        //根据管理员的地区获取
        if(isSup.equals("0") && !flag){//普通管理员     获取该活动下本地区
            res = workMapper.getWorksCounts(ACTIVITY_ID,DISTRICT_ID);
        }else {//超级管理员      获取该活动下全部的
            res = workMapper.getWorksCount(ACTIVITY_ID);

        }
        return res;
    }

    @Override
    public Administrators login(Administrators administrators) {
        return administratorsMapper.login(administrators);
    }

    @Override
    public Activity getActivityId(int ACTIVITY_ID) {
        Activity ac = activityMapper.getActivitySelect(ACTIVITY_ID);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
        ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
        if(ac.getSTAGE_IS_OPEN()==1) {//开启阶段
            if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
            }
            if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
            }
            if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
            }
            if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
            }
        }
        return ac;
    }

    @Override
    public List<UserWorkVo> getWorks(int ACTIVITY_ID,String isSup,int DISTRICT_ID,boolean flag) {
        List<UserWorkVo> u=null;
        if(isSup.equals("0") && !flag){//普通管理员     获取该活动下本地区
            u= workMapper.getWorkss(ACTIVITY_ID,DISTRICT_ID);
        }else {//超级管理员      获取该活动下全部的
            u= workMapper.getWorks(ACTIVITY_ID);

        }

        for(UserWorkVo uw:u){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            uw.setTIME(formatter.format(uw.getWORK_UPTIME()));
        }
        return u;
    }

    @Override
    public List<UserWorkVo> getWorkDowmLoad(UserWorkVo userWorkVo) {
        return workMapper.getWorkDowmLoad(userWorkVo);
    }

    @Override
    public List<UserWorkVo> WorkDowmLoad(int WORK_ID) {
        List<UserWorkVo> work =  workMapper.WorkDowmLoad(WORK_ID);

        for(UserWorkVo uwv : work){
            if("2".equals(uwv.getWORK_TYPE())){
                String[] URL = uwv.getRESOURCE_URL().split(",");//分割出来的字符数组
                List<Map<String ,String >> list =new ArrayList<>();
                for(int i = 0; i<URL.length;i++){
                    Map<String,String> map =new HashMap<String, String>();
                    map.put("url",URL[i]);
                    list.add(map);
                }
                uwv.setURL(list);
            }
        }
        return work;
    }

    @Override
    public String getActivityName(int ACTIVITY_ID) {
        return activityMapper.getActivityName(ACTIVITY_ID);
    }

    @Override
    public String getUserName(String USER_ID) {
        return userMapper.getUserName(USER_ID);
    }

    @Override
    public List<UserWorkVo> getWorkDowmLoads(UserWorkVo userWorkVo) {
        return workMapper.getWorkDowmLoads(userWorkVo);
    }

    @Override
    public PageInfo<Activity> getAllActivity(int pageNum, int pageSize) {
        try {
            //开启分页
            PageHelper.startPage(pageNum,pageSize);
            List<Activity> lists=activityMapper.getAllActivity();

            for(Activity ac:lists){
                int ACTIVITY_ID = ac.getACTIVITY_ID();
                ac.setAutherNumber(userMapper.getVoteUserNum(ACTIVITY_ID));//根据活动ID获取点赞人数
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
                ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
                if(ac.getSTAGE_IS_OPEN()==1) {//开启阶段
                    if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                        ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                        ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
                    }
                    if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                        ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                        ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
                    }
                    if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                        ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                        ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
                    }
                    if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                        ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                        ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
                    }
                }
            }

            PageInfo<Activity> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Activity> getActivityByStateAndName(String name, String state,int pageNum, int pageSize,Administrators ad) {
        try {
            //开启分页
            List<Activity> lists=null;
            PageHelper.startPage(pageNum,pageSize);
            if("1".equals(ad.getIS_SUPER_ADMIN())){//超级管理员获取所有的活动
                lists = activityMapper.getActivityByStateAndName(name,state);
            }else {//普通管理员获取本服务器的活动和超级管理员的活动
                if("100001".equals(ad.getSERVER_ID())){//本地服务器上的管理员。获取本服务器上的所有活动
                    lists = activityMapper.getActivityByStateAndNamelocalServer(name,state,ad.getSERVER_ID(),ad.getADMIN());
                }else{
                    lists = activityMapper.getActivityByStateAndNames(name,state,ad.getSERVER_ID());
                    for(int x = 0 ; x < lists.size() ; x++){
                        String serverId = lists.get(x).getSERVER_ID();
                        String isSup = lists.get(x).getIS_SUPER_ADMIN();
                        if("100001".equals(serverId) && "0".equals(isSup)){
                            lists.remove(x);
                        }
                    }
                }
            }

            for(Activity ac:lists){
                int ACTIVITY_ID = ac.getACTIVITY_ID();
                ac.setAutherNumber(userMapper.getVoteUserNum(ACTIVITY_ID));//根据活动ID获取点赞人数
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
                ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
                if(ac.getSTAGE_IS_OPEN()==1) {//开启阶段
                    if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                        ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                        ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
                    }
                    if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                        ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                        ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
                    }
                    if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                        ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                        ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
                    }
                    if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                        ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                        ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
                    }
                }
            }

            PageInfo<Activity> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional
    public int addActivity(Activity activity,MpInfo mp) {
        activity.setACTIVITY_STATE("1");
        String path = FileUtils.saveActivityHtml(activity.getACTIVITY_DETAILS(),Utils.getName()+".html",mp);
        activity.setACTIVITY_DETAILS(path);
        return activityMapper.addActivity(activity);
    }

    @Override
    public Activity getActivityById(int ACTIVITY_ID) {
        return activityMapper.getActivityById(ACTIVITY_ID);
    }

    @Override
    @Transactional
    public int updateActivityById(Activity activity, MpInfo mp) {
        return activityMapper.updateActivityById(activity);
    }
    
    @Override
    @Transactional
    public int updateActivityDetailsById(Activity activity, MpInfo mp) {
        String path = FileUtils.saveActivityHtml(activity.getACTIVITY_DETAILS(), Utils.getName() + ".html", mp);
        activity.setACTIVITY_DETAILS(path);
        return activityMapper.updateActivityDetailsById(activity);
    }

    @Override
    @Transactional
    public int updateAc(Activity activity, MpInfo mp) {
        String path = FileUtils.saveActivityHtml(activity.getACTIVITY_DETAILS(), Utils.getName() + ".html", mp);
        activity.setACTIVITY_DETAILS(path);
        return activityMapper.updateAc(activity);
    }
    
    @Override
    @Transactional
    public int updateQrcode(Activity activity) {
        return activityMapper.updateQrcode(activity);
    }

    public List<DistrictList> getCountys(int id){
        return districtListMapper.getCountyid(id);//第一等级
    }

    @Override
    public int getAllAdmins(int id) {
        //PageHelper.startPage(page,limit);
        List<Administrators> lists=administratorsMapper.getAdmin(id);
        //首先获取自己
        //根据自己的等级获取下一等级的所有管理员
        List<Administrators> ad1= administratorsMapper.getAllAdmin(lists.get(0).getID());//第一等级
        for(Administrators aa: ad1 ){
            //将这一等级添加到lists
            lists.add(aa);
            List<Administrators> ad2= administratorsMapper.getAllAdmin(aa.getID());//第二等级
            for(Administrators ab: ad2){
                //将这一等级添加到lists
                lists.add(ab);
                List<Administrators> ad3= administratorsMapper.getAllAdmin(ab.getID());//第三等级
                for(Administrators ac: ad3){
                    //将这一等级添加到lists
                    lists.add(ac);
                    List<Administrators> ad4= administratorsMapper.getAllAdmin(ac.getID());//第四等级
                    for(Administrators ad: ad4){
                        //将这一等级添加到lists
                        if(ad!=null) {
                            lists.add(ad);
                        }
                    }
                }
            }
        }
        //PageInfo<Administrators> list= new PageInfo<>(lists);
       int list =  lists.size();

        return list;
    }

    @Override
    public List<Administrators> getAllAdmin(int id,int page, int limit) {
        //PageHelper.startPage(page,limit);
        List<Administrators> lists=administratorsMapper.getAdmin(id);
        //首先获取自己
        //根据自己的等级获取下一等级的所有管理员
        List<Administrators> ad1= administratorsMapper.getAllAdmin(lists.get(0).getID());//第一等级
        for(Administrators aa: ad1 ){
            //将这一等级添加到lists
            lists.add(aa);
            List<Administrators> ad2= administratorsMapper.getAllAdmin(aa.getID());//第二等级
            for(Administrators ab: ad2){
                //将这一等级添加到lists
                lists.add(ab);
                List<Administrators> ad3= administratorsMapper.getAllAdmin(ab.getID());//第三等级
                for(Administrators ac: ad3){
                    //将这一等级添加到lists
                    lists.add(ac);
                    List<Administrators> ad4= administratorsMapper.getAllAdmin(ac.getID());//第四等级
                    for(Administrators ad: ad4){
                        //将这一等级添加到lists
                        if(ad!=null) {
                            lists.add(ad);
                        }
                    }
                }
            }
        }
        //PageInfo<Administrators> list= new PageInfo<>(lists);
        List<Administrators> list =null;
        int count = lists.size();//获取总数
        int start =page*limit-20;//开始数量
        int end =page*limit;//结束数量
        if(count>end){//总量 大于请求数量  用请求数量
            list = lists.subList( start, end);
        }else{
            list = lists.subList( start, count);
        }

        return list;
    }

    @Override
    public Administrators getAdminById(int ID) {
        return administratorsMapper.getAdminById(ID);
    }

    @Override
    @Transactional
    public int updateAdmin(Administrators administrators) {
        return administratorsMapper.updateAdmin(administrators);
    }

    @Override
    public List<DistrictList> getCounty() {
        return districtListMapper.getCounty();
    }

    @Override
    @Transactional
    public int addAdmin(Administrators administrators,int id,int level,boolean falg,Administrators ad) {
        if("1".equals(administrators.getIS_SUPER_ADMIN())){//超级管理员添加信息
            if(!falg){//不是创建本地管理员
                Logo logo = new Logo();
                logo.setSERVER_ID(administrators.getSERVER_ID());
                logo.setADMIN(administrators.getADMIN_ACCOUNT());
                //超级管理员添加需要添加服务器信息
                logoMapper.insertLogo(logo);
            }
            if("100001".equals(administrators.getSERVER_ID())){
                Logo logo = new Logo();
                logo.setSERVER_ID("100001");
                logo.setADMIN(administrators.getADMIN_ACCOUNT());
                //超级管理员添加需要添加服务器信息
                logoMapper.insertLogo(logo);

            }
            logoMapper.insertProbation(administrators.getADMIN_ACCOUNT(),administrators.getSERVER_ID());
            administrators.setADMIN(administrators.getADMIN_ACCOUNT());
            //修改地址表信息
            districtListMapper.updateDistrictAdmin(administrators.getDISTRICT_ID(),administrators.getADMIN_ACCOUNT());




        }else{
            administrators.setADMIN(ad.getADMIN());
        }
        administrators.setLEVEL(level+1);
        administrators.setPARENT_ID(id);
        return administratorsMapper.addAdmin(administrators);
    }

    @Override
    @Transactional
    public int deleteAdmin(String ADMIN_ACCOUNT,Administrators ads) {

        //获取被删除管理员的id
        Administrators ids= administratorsMapper.getAdminId(ADMIN_ACCOUNT);
        //PageHelper.startPage(page,limit);
        List<Administrators> lists=administratorsMapper.getAdmin(ids.getID());
        //首先获取自己
        //根据自己的等级获取下一等级的所有管理员
        List<Administrators> ad1= administratorsMapper.getAllAdmin(lists.get(0).getID());//第一等级
        for(Administrators aa: ad1 ){
            //将这一等级添加到lists
            lists.add(aa);
            List<Administrators> ad2= administratorsMapper.getAllAdmin(aa.getID());//第二等级
            for(Administrators ab: ad2){
                //将这一等级添加到lists
                lists.add(ab);
                List<Administrators> ad3= administratorsMapper.getAllAdmin(ab.getID());//第三等级
                for(Administrators ac: ad3){
                    //将这一等级添加到lists
                    lists.add(ac);
                    List<Administrators> ad4= administratorsMapper.getAllAdmin(ac.getID());//第四等级
                    for(Administrators ad: ad4){
                        //将这一等级添加到lists
                        if(ad!=null) {
                            lists.add(ad);
                        }
                    }
                }
            }
        }
        int res = 0;
        //改版新    将redis中的活动信息写入数据库
        // redisService.setActivityDetailFromRedisToSqlServer(ads.getLAST_ACTIVITY_ID());
        //改版新    将redis中的作品信息写入数据库
        // redisService.setWorkDetailFromRedisToSqlServer(ads.getLAST_ACTIVITY_ID());
        for(Administrators ad:lists){
            //获取本管理员的信息
            AdminDelete adminDelete = new AdminDelete();
            adminDelete.setADMIN_ID(ads.getID());
            adminDelete.setADMIN_ACCOUNT(ads.getADMIN_ACCOUNT());
            adminDelete.setADMIN_NAME(ads.getADMIN_NAME());
            adminDelete.setADMIN_PHONE(ads.getPHONE());
            adminDelete.setBE_ADMIN_ID(ad.getID());
            adminDelete.setBE_ADMIN_ACCOUNT(ad.getADMIN_ACCOUNT());
            adminDelete.setBE_ADMIN_NAME(ad.getADMIN_NAME());
            adminDelete.setBE_ADMIN_PHONE(ad.getPHONE());
            //将该管理员中的活动删除
            int deleteActivity = activityMapper.deleteActivityByAdminAccount(ad.getADMIN_ACCOUNT());

            adminDeleteMapper.insertDeleteMesg(adminDelete);
            res = administratorsMapper.deleteAdmin(ad.getADMIN_ACCOUNT());
        }
        return res;
    }

    @Override
    @Transactional
    public Integer addPrize(Prize prize) {
        return prizeMapper.addPrize(prize);
    }

    @Override
    public Integer addGroupRule(GroupRule groupRule) {
        return groupRuleMapper.addGroupRule(groupRule);
    }

    @Override
    public PageInfo<Prize> getAllPrize(int pageNum, int pageSize) {
        try {
            //开启分页
            PageHelper.startPage(pageNum,pageSize);
            List<Prize> lists=prizeMapper.getAllPrize();
            PageInfo<Prize> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public UserWorkVo getWork(int ID) {
        return workMapper.getWork(ID);
    }

    @Override
    public PageInfo<DistrictListUserWorkVo> getUserRegInfo(int page, int limit, String USER_NAME, Administrators ad) {
        try {
            //开启分页
            Activity activity = activityMapper.getActivityId(ad.getLAST_ACTIVITY_ID());
            PageHelper.startPage(page,limit);
            List<DistrictListUserWorkVo> lists=null;
            if(ad.getIS_SUPER_ADMIN().equals("1") || ad.getID().equals(activity.getADMIN_ID())){//普通管理员     获取该活动下本地区
                lists=userMapper.getUserRegInfo(USER_NAME,ad.getLAST_ACTIVITY_ID());
            } else {//超级管理员      获取该活动下全部的
                Integer districtId = ad.getDISTRICT_ID();
                List<Integer> districtIdList = new ArrayList<>();
                districtIdList.add(districtId);
                getChildrenIds(districtId, districtIdList);
                lists=userMapper.getUserRegInfos(USER_NAME,ad.getLAST_ACTIVITY_ID(), districtIdList);
            }
            for(DistrictListUserWorkVo dl:lists){
                List<UserWorkVo> list=workMapper.getWorkByUserId(dl.getUSER_ID(),dl.getACTIVITY_ID());
                int num =0;
                for(UserWorkVo uw:list){
                    num+=uw.getVOTES_NUMBER();
                }
                dl.setVOTESNUM(num);
            }
            PageInfo<DistrictListUserWorkVo> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public List<DistrictListUserWorkVo> getAllUserRegInfo(String USER_NAME, Administrators ad) {
        try {
            //开启分页
            Activity activity = activityMapper.getActivityId(ad.getLAST_ACTIVITY_ID());
            List<DistrictListUserWorkVo> lists = null;
            if (ad.getIS_SUPER_ADMIN().equals("1") || ad.getID().equals(activity.getADMIN_ID())) {//普通管理员     获取该活动下本地区
                lists = userMapper.getUserRegInfo(USER_NAME, ad.getLAST_ACTIVITY_ID());
            } else {//超级管理员      获取该活动下全部的
                Integer districtId = ad.getDISTRICT_ID();
                List<Integer> districtIdList = new ArrayList<>();
                districtIdList.add(districtId);
                getChildrenIds(districtId, districtIdList);
                lists = userMapper.getUserRegInfos(USER_NAME, ad.getLAST_ACTIVITY_ID(), districtIdList);
            }
            String tSQL = "select * from ACTIVITY_APPRAISAL_TAG";
            Map<String, Integer> tMap = new HashMap<>();
            List<Map<String, Object>> tList = jdbcTemplate.queryForList(tSQL);
            for (Map<String, Object> map : tList) {
                String key = map.get("ACTIVITY_ID") + "_" + map.get("APPRAISAL_USER_ID");
                if (tMap.containsKey(key)) {
                    Integer value = tMap.get(key);
                    value++;
                } else {
                    tMap.put(key, 1);
                }
            }

            String testSQL = "SELECT his.end_level_big_id,his.score,his.quiz_time,his.APPRAISAL_USER_ID,his.APPRAISAL_HISTORY_TAG,aat.ACTIVITY_ID FROM [classificationofreading].[dbo].[abss_appraisal_history] his left join ACTIVITY_APPRAISAL_TAG aat on his.APPRAISAL_HISTORY_TAG = aat.APPRAISAL_HISTORY_TAG WHERE his.APPRAISAL_HISTORY_TAG in (SELECT t1.APPRAISAL_HISTORY_TAG FROM ACTIVITY_APPRAISAL_TAG t1 JOIN (SELECT ACTIVITY_ID,APPRAISAL_USER_ID, MAX(APPRAISAL_TIME) AS APPRAISAL_TIME FROM ACTIVITY_APPRAISAL_TAG GROUP BY ACTIVITY_ID,APPRAISAL_USER_ID) t2 ON t1.ACTIVITY_ID = t2.ACTIVITY_ID AND t1.APPRAISAL_USER_ID = t2.APPRAISAL_USER_ID AND t1.APPRAISAL_TIME = t2.APPRAISAL_TIME)";
            Map<String, Map<String, Object>> testMap = new HashMap<>();
            List<Map<String, Object>> testList = jdbcTemplate.queryForList(testSQL);
            for (Map<String, Object> map : testList) {
                String key = map.get("ACTIVITY_ID") + "_" + map.get("APPRAISAL_USER_ID");
                if (!testMap.containsKey(key)) {
                    testMap.put(key, map);
                }
            }
            for (DistrictListUserWorkVo dl : lists) {
                String key = dl.getACTIVITY_ID() + "_" + dl.getAPPRAISAL_USER_ID();
                if (tMap.containsKey(key)) {
                    dl.setTEST_TOTAL(tMap.get(key)+"");
                } else {
                    dl.setTEST_TOTAL("0");
                }
                if (testMap.containsKey(key)) {
                    Map<String, Object> map = testMap.get(key);
                    String levelId = map.get("end_level_big_id") + "";
                    if (null == levelId || "".equals(levelId) || "null".equalsIgnoreCase(levelId)) {
                        dl.setTEST_LEVEL(null);
                    } else {
                        dl.setTEST_LEVEL(Integer.valueOf(levelId) - 1);
                    }
                    String score = map.get("score") + "";
                    if (null == score || "".equals(score) || "null".equalsIgnoreCase(score)) {
                        dl.setTEST_SCORE(null);
                    } else {
                        dl.setTEST_SCORE(Double.valueOf(score));
                    }
                    dl.setTEST_QUIZ_TIME(map.get("quiz_time") + "");
                } else {
                    continue;
                }
            }
            return lists;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional
    public List<UserVotesInfo> getUserVotesList(UserVotesInfo userVotesInfo) {
        return votesMapper.getUserVotesList(userVotesInfo);
    }

    @Override
    @Transactional
    public int deleteUserVote(String ID, Administrators ad) {
        String REMOVER = ad.getADMIN_NAME();
        return votesMapper.deleteUserVote(ID, REMOVER);
    }


    public int addMessage(Message message, UserRegInfo userRegInfo) {
        userMapper.updateUserRegStatus(userRegInfo);
        return messageMapper.addMessage(message);
    }

    @Override
    public List<AuditFailComments> getFail(String WORK_TYPE) {
        return workMapper.getFail(WORK_TYPE);
    }

    @Override
    @Transactional
    public int audit(int AUDIT_STATE, int WORK_ID,String COMMENT) {
        int CURRENT_STAGE=0;
        if (AUDIT_STATE==1) {//审核通过   CURRENT_STAGE =>  1  当前阶段 审核前为0 审核通过后变成1 代表处于第一阶段 以后每进入一个新阶段 该值加 1 审核未通过为 -1
            CURRENT_STAGE = 1;
        }else {
            CURRENT_STAGE = -1;
        }
        return workMapper.audit(AUDIT_STATE, WORK_ID, COMMENT,CURRENT_STAGE);
    }

    @Override
    public Administrators checkDistrict(Administrators administrators) {
        return administratorsMapper.checkDistrict(administrators);
    }

    @Override
    public int checkUserName(Administrators administrators) {
        return administratorsMapper.checkUserName(administrators);
    }

    @Override
    public PageInfo<UserWorkVo> getWorkByActivityId(int page,int limit,int DISTRICT_ID,int IS_LOCAL,int AUDIT_STATE,String WORK_NAME,int ACTIVITY_ID ,String isSup,int districtId) {

        Activity activity = activityMapper.getActivityById(ACTIVITY_ID);
        try {
            //开启分页
            PageHelper.startPage(page,limit);
            List<UserWorkVo> lists=null;
            int is_host = 1;
            if(IS_LOCAL==1)is_host=0;
            if(isSup.equals("0")){//普通管理员
                    // 获取该活动下本地区
                    lists=workMapper.getWorkByActivityIds(AUDIT_STATE,WORK_NAME,ACTIVITY_ID,districtId);
               // }
            }else {//超级管理员      获取该活动下全部的
                //System.out.println("超级管理员");
                lists=workMapper.getWorkByActivityId(DISTRICT_ID,AUDIT_STATE,WORK_NAME,ACTIVITY_ID);
            }

            for(UserWorkVo uwv : lists){
                if(activity.getDELETED()==0 && activity.getREMOVE()==0) {
                    uwv.setVOTES_NUMBER(redisService.getWOrkVotesNumber(ACTIVITY_ID,uwv.getWORK_ID()));
                }
                uwv.setDISTRICT_NAME(districtListMapper.getCountyName(uwv.getDISTRICT_ID()));
            }
            for(UserWorkVo uwv : lists){
                if("2".equals(uwv.getWORK_TYPE())){
                    String[] URL = uwv.getRESOURCE_URL().split(",");//分割出来的字符数组
                    List<Map<String ,String >> list =new ArrayList<>();
                    for(int i = 0; i<URL.length;i++){
                        Map<String,String> map =new HashMap<String, String>();
                        map.put("url",URL[i]);
                        list.add(map);
                    }
                    uwv.setURL(list);
                }
            }
            Collections.sort(lists);
            PageInfo<UserWorkVo> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<UserWorkVo> getWorkByActivityIds(int page,int limit,int DISTRICT_ID,int IS_LOCAL,int AUDIT_STATE,String WORK_NAME,int ACTIVITY_ID ,String isSup,int districtId) {

        Activity activity = activityMapper.getActivityById(ACTIVITY_ID);
        try {
            //开启分页
            PageHelper.startPage(page,limit);
            List<UserWorkVo> lists=null;
            if(isSup.equals("0")){//普通管理员
                // 获取该活动下本地区
                lists=workMapper.getWorkByActivityIdss(WORK_NAME,ACTIVITY_ID);
                // }
            }else {//超级管理员      获取该活动下全部的
                //System.out.println("超级管理员");
                lists=workMapper.getWorkByActivityIdssa(DISTRICT_ID,WORK_NAME,ACTIVITY_ID);
            }
            for(UserWorkVo uwv : lists){
                if(activity.getDELETED()==0 && activity.getREMOVE()==0) {
                    uwv.setVOTES_NUMBER(redisService.getWOrkVotesNumber(ACTIVITY_ID,uwv.getWORK_ID()));
                }
                uwv.setDISTRICT_NAME(districtListMapper.getCountyName(uwv.getDISTRICT_ID()));
            }
            for(UserWorkVo uwv : lists){
                if("2".equals(uwv.getWORK_TYPE())){
                    String[] URL = uwv.getRESOURCE_URL().split(",");//分割出来的字符数组
                    List<Map<String ,String >> list = new ArrayList<>();
                    for(int i = 0; i<URL.length;i++){
                        Map<String,String> map =new HashMap<String, String>();
                        map.put("url",URL[i]);
                        list.add(map);
                    }
                    uwv.setURL(list);
                }
            }
            Collections.sort(lists);
            PageInfo<UserWorkVo> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public PageInfo<UserWorkVo> getWorkByActivityIdss(int page,int limit,int DISTRICT_ID,int IS_LOCAL,int AUDIT_STATE,String WORK_NAME,int ACTIVITY_ID ,String isSup,int districtId) {

        Activity activity = activityMapper.getActivityById(ACTIVITY_ID);
        try {
            //开启分页
            PageHelper.startPage(page,limit);
            List<UserWorkVo> lists = null;
            if(isSup.equals("0")){//普通管理员
                // 获取该活动下本地区
                lists=workMapper.getWorkByActivityIdsss(WORK_NAME,ACTIVITY_ID);
                // }
            }else {//超级管理员      获取该活动下全部的
                //System.out.println("超级管理员");
                lists=workMapper.getWorkByActivityIdsssa(DISTRICT_ID,WORK_NAME,ACTIVITY_ID);
            }
            for(UserWorkVo uwv : lists){
                if(activity.getDELETED()==0 && activity.getREMOVE()==0) {
                    uwv.setVOTES_NUMBER(redisService.getWOrkVotesNumber(ACTIVITY_ID,uwv.getWORK_ID()));
                }
                uwv.setDISTRICT_NAME(districtListMapper.getCountyName(uwv.getDISTRICT_ID()));
            }
            for(UserWorkVo uwv : lists){
                if("2".equals(uwv.getWORK_TYPE())){
                    String[] URL = uwv.getRESOURCE_URL().split(",");//分割出来的字符数组
                    List<Map<String ,String >> list = new ArrayList<>();
                    for(int i = 0; i<URL.length;i++){
                        Map<String,String> map =new HashMap<String, String>();
                        map.put("url",URL[i]);
                        list.add(map);
                    }
                    uwv.setURL(list);
                }
            }
            for(UserWorkVo uwv : lists){
                uwv.setPRIZE_NAME(prizeMapper.getPrizeName(uwv.getPRIZE_ID()));
            }
            Collections.sort(lists);
            PageInfo<UserWorkVo> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public PageInfo<UserWorkVo> getWorkByActivityIdsss(int page,int limit,int DISTRICT_ID,int IS_LOCAL,int AUDIT_STATE,String WORK_NAME,int ACTIVITY_ID ,String isSup,int districtId) {

        Activity activity = activityMapper.getActivityById(ACTIVITY_ID);
        try {
            //开启分页
            PageHelper.startPage(page,limit);
            List<UserWorkVo> lists = null;
            if(isSup.equals("0")){//普通管理员
                // 获取该活动下本地区
                lists=workMapper.getWorkByActivityIdssss(WORK_NAME,ACTIVITY_ID);
                // }
            }else {//超级管理员      获取该活动下全部的
                //System.out.println("超级管理员");
                lists=workMapper.getWorkByActivityIdsssaa(DISTRICT_ID,WORK_NAME,ACTIVITY_ID);
            }
            for(UserWorkVo uwv : lists){
                if(activity.getDELETED()==0 && activity.getREMOVE()==0) {
                    uwv.setVOTES_NUMBER(redisService.getWOrkVotesNumber(ACTIVITY_ID,uwv.getWORK_ID()));
                }
                uwv.setDISTRICT_NAME(districtListMapper.getCountyName(uwv.getDISTRICT_ID()));
            }
            for(UserWorkVo uwv : lists){
                if("2".equals(uwv.getWORK_TYPE())){
                    String[] URL = uwv.getRESOURCE_URL().split(",");//分割出来的字符数组
                    List<Map<String ,String >> list = new ArrayList<>();
                    for(int i = 0; i<URL.length;i++){
                        Map<String,String> map =new HashMap<String, String>();
                        map.put("url",URL[i]);
                        list.add(map);
                    }
                    uwv.setURL(list);
                }
            }
            for(UserWorkVo uwv : lists){
                uwv.setPRIZE_NAME(prizeMapper.getPrizeName(uwv.getPRIZE_ID()));
            }
            Collections.sort(lists);
            PageInfo<UserWorkVo> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<UserWorkVo> getList(int activity_id, int districtId, String isSup,String id) {
        List<UserWorkVo> lists=null;
        if(isSup.equals("0")){//普通管理员     获取该活动下本地区
            if("1".equals(id)){//本区
                lists=workMapper.getWorkByActivityIdsExport(activity_id,districtId);
            }else if("2".equals(id)){//提交
                lists=workMapper.getWorkByActivityIdsExports(activity_id);
            }else if("3".equals(id)){
                lists=workMapper.getWorkByActivityIdsExportss(activity_id);
            }else if("4".equals(id)){
                lists=workMapper.getWorkByActivityIdsExportsss(activity_id);
            }
        }else {//超级管理员      获取该活动下全部的
            //System.out.println("超级管理员");
            if("1".equals(id)) {//所有的   审核未审核提交未提交的
                lists=workMapper.getWorkByActivityIdExport(activity_id);
            }else if("2".equals(id)){//提交的
                lists=workMapper.getWorkByActivityIdExporta(activity_id);
            }else if("3".equals(id)){
                lists=workMapper.getWorkByActivityIdExportaa(activity_id);
            }else if("4".equals(id)){
                lists=workMapper.getWorkByActivityIdExportaaa(activity_id);
            }

        }

        for(UserWorkVo uwv : lists){
            uwv.setDISTRICT_NAME(districtListMapper.getCountyName(uwv.getDISTRICT_ID()));
            if(uwv.getPRIZE_ID()!=null){
                uwv.setPRIZE_NAME(prizeMapper.getPrizeName(uwv.getPRIZE_ID()));
            }

            if("2".equals(uwv.getWORK_TYPE())){
                String[] URL = uwv.getRESOURCE_URL().split(",");//分割出来的字符数组
                List<Map<String ,String >> list =new ArrayList<>();
                for(int i = 0; i<URL.length;i++){
                    Map<String,String> map =new HashMap<String, String>();
                    map.put("url",URL[i]);
                    list.add(map);
                }
                uwv.setURL(list);
            }
        }
        return lists;
    }

    @Override
    @Transactional
    public int delWork(int WORK_ID) {
        return workMapper.delWork(WORK_ID);
    }

    @Override
    public int checkActivityByid(int ACTIVITY_ID) {
        return activityMapper.checkActivityByid(ACTIVITY_ID);
    }
    @Override
    public int checkActivityByids(int ACTIVITY_ID,String server_id) {
        return activityMapper.checkActivityByids(ACTIVITY_ID,server_id);
    }

    @Override
    public int getNewAvtivityId() {
        return activityMapper.getNewAvtivityId();
    }

    @Override
    public PageInfo<User> getUser(int page, int limit, String NICKNAME,Administrators ad) {
        try {
           //开启分页
            PageHelper.startPage(page,limit);
            List<User> lists=null;
            if("1".equals(ad.getIS_SUPER_ADMIN())){//超级管理员获取所有的
                lists=userMapper.getUserPage(NICKNAME);
            }else{//普通管理员获取本系统的
                lists=userMapper.getUserPages(NICKNAME,ad.getSERVER_ID());
            }
            PageInfo<User> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public List<String> getUsers() {
        List<User> lists = userMapper.getUserPage(null);
        List<String> list =new ArrayList<>();
        for(int x = 0 ; x<lists.size();x++){
            String url = null;
            if(lists.get(x).getHEAD_IMG_URL()!=null){
                url = lists.get(x).getHEAD_IMG_URL().replace("132", "0");
            }
            list.add(x,url);
        }
        return list;
    }

    @Override
    public List<Prize> getPrizeWorkListByActivityId(int activity_id) {


        List<Prize> prizeList=prizeMapper.getPrizeListByActivityId(activity_id);

        prizeList.forEach((item)->{
            item.setWorkList(workMapper.getWorkByPrizeId(item.getPRIZE_ID()));

        });

        return  prizeList;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addPrizeList(List<Prize> prizes) {
        int ret=0;
        for(int i=0;i<prizes.size();i++){
                ret+=prizeMapper.addPrize(prizes.get(i));
        }
        return ret;

    }

    @Override
    @Transactional
    public int setLastActivityId(String ADMIN_ACCOUNT, int ACTIVITY_ID) {
        return administratorsMapper.setLastActivityId(ADMIN_ACCOUNT,ACTIVITY_ID);
    }

    @Override
    public Administrators getlogin(String ADMIN_ACCOUNT) {
        return administratorsMapper.getlogin(ADMIN_ACCOUNT);
    }

    @Override
    public ActivityPrizeVo getActivityDetails(int ACTIVITY_ID) {
        ActivityPrizeVo ac = activityMapper.getActivityDetails(ACTIVITY_ID);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
        ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
        if (ac.getACTIVITY_ENTRYTIME() != null) {
            ac.setENTRYTIME(formatter.format(ac.getACTIVITY_ENTRYTIME()));
        }
        if(ac.getSTAGE_IS_OPEN()==1) {//开启阶段
            if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
            }
            if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
            }
            if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
            }
            if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
            }
        }
        return ac;
    }


    @Override
    public Administrators checkDistrict2(Administrators administrators) {
        return administratorsMapper.checkDistrict2(administrators);
    }

    @Override
    public List<String> getPrizeNames() {
        return prizeMapper.getPrizeNames();
    }


    @Override
    @Transactional
    public int setWork(int WORK_ID,Administrators ad) {
        int res=0;
        //根据活动id查询该活动上传数量
        int SUBMIT_WORK_NUMBER = activityMapper.getSubmitWorkNumber(ad.getLAST_ACTIVITY_ID());
        //该活动下此地区提交作品数量
        int count=0;
        if("1".equals(ad.getIS_SUPER_ADMIN())) {//超级管理员提交作品
            //获取该作品所在的地址id
            UserWorkVo work =workMapper.getWorksss(WORK_ID);
            count = activityMapper.getCountNumber(ad.getLAST_ACTIVITY_ID(), work.getDISTRICT_ID());
        }else{//普通管理员提交作品
            count = activityMapper.getCountNumber(ad.getLAST_ACTIVITY_ID(), ad.getDISTRICT_ID());
        }
        if(SUBMIT_WORK_NUMBER>count){//提交数量小于设置数量
            res=workMapper.setWork(WORK_ID);
        }else{
            res=2;
        }
        return res;
    }

    @Override
    @Transactional
    public int deWork(int WORK_ID) {
        return workMapper.deWork(WORK_ID);
    }

    @Override
    @Transactional
    public int promotionWork(int WORK_ID) {
        return workMapper.promotionWork(WORK_ID);
    }

    @Override
    public List<Activity> getAllActivities() {
        return activityMapper.getAllActivity();
    }

    @Override
    public List<Activity> getAllActivitiesNoRemove() {
        return activityMapper.getAllActivitiesNoRemove();
    }

    @Override
    public List<Activity> getAllActivitiesNoRemoves(String server_id) {
        List<Activity> lists = activityMapper.getAllActivitiesNoRemoves(server_id);
        for(int x = 0 ; x < lists.size() ; x++){
            String serverId = lists.get(x).getSERVER_ID();
            String isSup = lists.get(x).getIS_SUPER_ADMIN();
            if("100001".equals(serverId) && "0".equals(isSup)){
                lists.remove(x);
            }
        }
        return  lists;
    }
    @Override
    public List<Activity> getAllActivitiesNoRemovess(String server_id, String admin) {
        return activityMapper.getAllActivitiesNoRemovess(server_id,admin);
    }
    @Override
    public int getActivityAdminId(int last_activity_id) {
        return activityMapper.getActivityAdminId(last_activity_id);
    }

    @Override
    public boolean isSubordinates(int activityAdminId,int id) {
        List<Administrators> lists=administratorsMapper.getAdmin(activityAdminId);
        List<Administrators> ad1= administratorsMapper.getAllAdmin(lists.get(0).getID());//第一等级
        for(Administrators aa: ad1 ){
            //将这一等级添加到lists
            lists.add(aa);
            List<Administrators> ad2= administratorsMapper.getAllAdmin(aa.getID());//第二等级
            for(Administrators ab: ad2){
                //将这一等级添加到lists
                lists.add(ab);
                List<Administrators> ad3= administratorsMapper.getAllAdmin(ab.getID());//第三等级
                for(Administrators ac: ad3){
                    //将这一等级添加到lists
                    lists.add(ac);
                    List<Administrators> ad4= administratorsMapper.getAllAdmin(ac.getID());//第四等级
                    for(Administrators ad: ad4){
                        //将这一等级添加到lists
                        if(ad!=null) {
                            lists.add(ad);
                        }
                    }
                }
            }
        }
        boolean res = false;
        for(Administrators ad : lists){
            if(ad.getID().equals(id)){
                res =true;
                break;
            }
        }
        return res;
    }
    @Override
    public boolean isSubordinate(int activityAdminId,int id) {
        boolean res = false;
        if(activityAdminId==id){
            res =true;
        }
        return res;
    }

    @Override
    public String getDisName(Integer district_id) {
        return districtListMapper.getCountyName(district_id);
    }

    @Override
    public boolean islastAdmin(Integer id) {
        boolean res = false;
        int level = administratorsMapper.getLevel(id);
        if(level==4){//最低级管理员
            res=true;
        }
        return res;
    }

    @Override
    @Transactional
    public int backUploadWork(UserWorkVo userWorkVo) {
        String USER_ID = Utils.getAddressID() + new Date().getTime();
        //获取活动的severid
        String Server_ID = mpInFoMapper.getServerId(userWorkVo.getACTIVITY_ID());

        USER_ID = Server_ID + "-" + USER_ID;//后台上传作品时添加报名表和用户表

        // System.out.println(USER_ID);
        if ("null".equals(userWorkVo.getWORK_LENTH())) {
            userWorkVo.setWORK_LENTH(0);
        }
        //创建用户
        userWorkVo.setUSER_ID(USER_ID);
        int s = userMapper.createUser(userWorkVo);//添加用户报名表
        int u = userMapper.addUserBack(userWorkVo);//添加用户表信息
        //创建作品
        int w = workMapper.createWork(userWorkVo);//添加作品表数据
        Work work = new Work();
        work.setWORK_ID(userWorkVo.getWORK_ID());
        work.setACTIVITY_ID(userWorkVo.getACTIVITY_ID());
        redisService.addWork(work);
        //作品加一
        //redis中报名信息加一
        redisService.setREG_USER(userWorkVo.getACTIVITY_ID());
        return w;
    }
    
    @Override
    @Transactional
    public int setIsOpenYdcp(int is_open_ydcp, int ACTIVITY_ID) {
        return activityMapper.setIsOpenYdcp(is_open_ydcp, ACTIVITY_ID);
    }

    @Override
    @Transactional
    public int setYdcpNum(int ydcp_num, int ACTIVITY_ID) {
        return activityMapper.setYdcpNum(ydcp_num, ACTIVITY_ID);
    }

    @Override
    @Transactional
    public int setOrderRule(int order_rule, int ACTIVITY_ID) {
        return activityMapper.setOrderRule(order_rule, ACTIVITY_ID);
    }

    @Override
    @Transactional
    public int deptUpdate(Activity activity) {
        return activityMapper.deptUpdate(activity);
    }

    @Override
    @Transactional
    public int groupRuleUpdate(Activity activity) {
        return activityMapper.groupRuleUpdate(activity);
    }

    @Override
    @Transactional
    public int deleteGroupRule(int ID) {
        return groupRuleMapper.deleteGroupRuleById(ID);
    }

    @Override
    @Transactional
    public int setIsVote(int is_vote,int ACTIVITY_ID) {
        return activityMapper.setIsVote(is_vote,ACTIVITY_ID);
    }

    @Override
    public List<DistrictListUserWorkVo> getAccountWorkDetails(int ACTIVITY_ID) {
        //获取活动中的服务器编号
        String Server_id = activityMapper.getActivityServerId(ACTIVITY_ID);
        //获取全部管理员（除超级管理员外）
        List<DistrictListUserWorkVo> lists=districtListMapper.getCountyByIda(1);
        if(Server_id.equals(lists.get(0).getSERVER_ID())) {//是超级管理员发布活动
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictListUserWorkVo> ad1 = districtListMapper.getCountyByIdas(lists.get(0).getID());//第一等级
            for (DistrictListUserWorkVo aa : ad1) {
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictListUserWorkVo> ad2 = districtListMapper.getCountyByIdas(aa.getID());//第二等级
                for (DistrictListUserWorkVo ab : ad2) {
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictListUserWorkVo> ad3 = districtListMapper.getCountyByIdas(ab.getID());//第三等级
                    for (DistrictListUserWorkVo ac : ad3) {
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictListUserWorkVo> ad4 = districtListMapper.getCountyByIdas(ac.getID());//第四等级
                        for (DistrictListUserWorkVo ad : ad4) {
                            //将这一等级添加到lists
                            if (ad != null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }
        }else{
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictListUserWorkVo> ad1 = districtListMapper.getCountyida(lists.get(0).getID(), Server_id);//第一等级
            for (DistrictListUserWorkVo aa : ad1) {
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictListUserWorkVo> ad2 = districtListMapper.getCountyida(aa.getID(), Server_id);//第二等级
                for (DistrictListUserWorkVo ab : ad2) {
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictListUserWorkVo> ad3 = districtListMapper.getCountyida(ab.getID(), Server_id);//第三等级
                    for (DistrictListUserWorkVo ac : ad3) {
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictListUserWorkVo> ad4 = districtListMapper.getCountyida(ac.getID(), Server_id);//第四等级
                        for (DistrictListUserWorkVo ad : ad4) {
                            //将这一等级添加到lists
                            if (ad != null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }
        }
        for(DistrictListUserWorkVo dis:lists){
            int DISTRICT_ID=dis.getID();
            int unumber = userMapper.getParticipantsCounts(ACTIVITY_ID,DISTRICT_ID);//报名人数
            dis.setUser_Number(unumber);
            int wnumber = workMapper.getWorksCounts(ACTIVITY_ID,DISTRICT_ID);//上传作品数
            dis.setWork_Number(wnumber);
            int anumber = workMapper.getAuditWorksCount(ACTIVITY_ID,DISTRICT_ID);//审核通过作品数
            dis.setAudit_Number(anumber);
            int snumber = workMapper.getSubmitWorksCount(ACTIVITY_ID,DISTRICT_ID);//提交作品数
            dis.setSubmit_Number(snumber);
            int fnumber = workMapper.getFinalWorksCount(ACTIVITY_ID,DISTRICT_ID);//进入决赛
            dis.setFinal_Number(fnumber);
            int Pnumber = workMapper.getPinalWorksCount(ACTIVITY_ID,DISTRICT_ID);//进入决赛
            dis.setPinal_Number(Pnumber);
            List<Work> works=workMapper.getWorksVotes(ACTIVITY_ID,DISTRICT_ID);//投票数
            int vnumber = 0;
            int lnumber=0;
            for (Work work:works){
                vnumber=vnumber+work.getVOTES_NUMBER();
                lnumber=lnumber+work.getWORK_VISIST();
            }
            dis.setVote_Number(vnumber);
            dis.setVisit_Number(lnumber);

        }
        return lists;
    }

    @Override
    @Transactional
    public int setWorkNumber(int works_limit_peruser, int activity_id) {
        return activityMapper.setWorkNumber(works_limit_peruser,activity_id);
    }

    @Override
    @Transactional
    public int setVotesLinitPerday(int votes_limit_perday, int activity_id) {
        return activityMapper.setVotesLinitPerday(votes_limit_perday,activity_id);
    }

    @Override
    @Transactional
    public int setVotesLimitTosinglePerday(int votes_limit_tosingle_perday, int activity_id) {
        return activityMapper.setVotesLimitTosinglePerday(votes_limit_tosingle_perday,activity_id);
    }

    @Override
    @Transactional
    public int setCurrentStage(int current_stage, int activity_id) {
        return activityMapper.setCurrentStage(current_stage, activity_id);
    }
    
    @Override
    @Transactional
    public int getWorkType(int V_TYPE, int A_TYPE, int F_TYPE, int P_TYPE) {
        int work_type = 0;
        if (V_TYPE == 1 && A_TYPE == 0 && F_TYPE == 0 && P_TYPE == 0) {// 视频
            work_type = 1;
        } else if (V_TYPE == 0 && A_TYPE == 1 && F_TYPE == 0 && P_TYPE == 0) {// 音频
            work_type = 2;
        } else if (V_TYPE == 1 && A_TYPE == 1 && F_TYPE == 0 && P_TYPE == 0) {// 视频 音频
            work_type = 3;
        } else if (V_TYPE == 0 && A_TYPE == 0 && F_TYPE == 1 && P_TYPE == 0) {// 范文
            work_type = 4;
        } else if (V_TYPE == 1 && A_TYPE == 0 && F_TYPE == 1 && P_TYPE == 0) {// 视频 范文
            work_type = 5;
        } else if (V_TYPE == 0 && A_TYPE == 1 && F_TYPE == 1 && P_TYPE == 0) {// 音频 范文
            work_type = 6;
        } else if (V_TYPE == 1 && A_TYPE == 1 && F_TYPE == 1 && P_TYPE == 0) {// 视频 音频 范文
            work_type = 7;
        } else if (V_TYPE == 0 && A_TYPE == 0 && F_TYPE == 0 && P_TYPE == 1) {// 图片
            work_type = 8;
        } else if (V_TYPE == 1 && A_TYPE == 0 && F_TYPE == 0 && P_TYPE == 1) {// 视频 图片
            work_type = 9;
        } else if (V_TYPE == 0 && A_TYPE == 1 && F_TYPE == 0 && P_TYPE == 1) {// 音频 图片
            work_type = 10;
        } else if (V_TYPE == 1 && A_TYPE == 1 && F_TYPE == 0 && P_TYPE == 1) {// 视频 音频 图片
            work_type = 11;
        } else if (V_TYPE == 0 && A_TYPE == 0 && F_TYPE == 1 && P_TYPE == 1) {// 范文 图片
            work_type = 12;
        } else if (V_TYPE == 1 && A_TYPE == 0 && F_TYPE == 1 && P_TYPE == 1) {// 视频 范文 图片
            work_type = 13;
        } else if (V_TYPE == 0 && A_TYPE == 1 && F_TYPE == 1 && P_TYPE == 1) {// 音频 范文 图片
            work_type = 14;
        } else if (V_TYPE == 1 && A_TYPE == 1 && F_TYPE == 1 && P_TYPE == 1) {// 视频 音频 范文 图片
            work_type = 15;
        }
        return work_type;
    }

    @Override
    @Transactional
    public int setWorkType(int V_TYPE,int A_TYPE,int F_TYPE,int P_TYPE, int activity_id) {
        int work_type = getWorkType(V_TYPE, A_TYPE, F_TYPE, P_TYPE);
        return activityMapper.setWorkType(work_type,activity_id);
    }

    @Override
    @Transactional
    public int setIsSigup(int is_sigup, int activity_id) {
        return activityMapper.setIsSigup(is_sigup,activity_id);
    }

    @Override
    @Transactional
    public int setSubmitWorkNumber(int submit_work_number, int activity_id) {
        return activityMapper.setSubmitWorkNumber(submit_work_number,activity_id);
    }

    @Override
    @Transactional
    public int deWpromotionWorkork(int work_id) {
        return workMapper.deWpromotionWorkork(work_id);
    }

    @Override
    public String getPicName(int work_id) {
        return workMapper.getPicName(work_id);
    }

    @Override
    @Transactional
    public int backEditorWork(Work work) {
        return workMapper.backEditorWork(work);
    }

    @Override
    public int getAnumber(int activity_id) {
        return workMapper.getAnumber(activity_id);
    }

    @Override
    public int getSnumber(int activity_id) {
        return workMapper.getSnumber(activity_id);
    }

    @Override
    public int getFnumber(int activity_id) {
        return workMapper.getFnumber(activity_id);
    }
    @Override
    public int getPnumber(int activity_id) {
        return workMapper.getPnumber(activity_id);
    }

    @Override
    @Transactional
    public int setDELETED(int deleted, int activity_id) {
        return activityMapper.setDELETED(deleted,activity_id);
    }

    @Override
    @Transactional
    public int setACTIVITY_STATE(int activity_state, int activity_id) {
        return activityMapper.setACTIVITY_STATE(activity_state,activity_id);
    }

    @Override
    @Transactional
    public int setIS_IN(int is_in, int activity_id) {
        return activityMapper.setIS_IN(is_in, activity_id);
    }

    @Override
    @Transactional
    public int setIS_SHOW_COMMENT(int is_show_comment, int activity_id) {
        return activityMapper.setIS_SHOW_COMMENT(is_show_comment, activity_id);
    }

    @Override
    public String getDistrictBizById(int id) {
        String bizCode = "";
        String sql = "select school_id,default_class_id from [classificationofreading].[dbo].[b_school] where school_id=?";
        List<DistrictList> districtList = districtListMapper.getCountyById(id);
        if (null != districtList && districtList.size() > 0) {
            DistrictList district = districtList.get(0);
            String schoolId = district.getSCHOOL_ID();
            List<Map<String, Object>> schoolList = jdbcTemplate.queryForList(sql, schoolId);
            if (null != schoolList && schoolList.size() > 0) {
                bizCode = schoolId +"-"+ schoolList.get(0).get("default_class_id");
            }
        }
        return bizCode;
    }

    @Override
    public int getAllDistricts(int id) {
        try {
            //开启分页
            //PageHelper.startPage(page,limit);
            //获取该管理员的地址
            List<DistrictList> lists=districtListMapper.getCountyById(id);
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictList> ad1= districtListMapper.getCountyid(lists.get(0).getID());//第一等级
            for(DistrictList aa: ad1 ){
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictList> ad2= districtListMapper.getCountyid(aa.getID());//第二等级
                for(DistrictList ab: ad2){
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictList> ad3= districtListMapper.getCountyid(ab.getID());//第三等级
                    for(DistrictList ac: ad3){
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictList> ad4= districtListMapper.getCountyid(ac.getID());//第四等级
                        for(DistrictList ad: ad4){
                            //将这一等级添加到lists
                            if(ad!=null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }
            // PageInfo<DistrictList> list= new PageInfo<>(lists);
            int list = lists.size();

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<DistrictList> getAllDistrict(int page, int limit,int id) {
        try {
            //开启分页
            //PageHelper.startPage(page,limit);
            //获取该管理员的地址
            List<DistrictList> lists=districtListMapper.getCountyById(id);
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictList> ad1= districtListMapper.getCountyid(lists.get(0).getID());//第一等级
            for(DistrictList aa: ad1 ){
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictList> ad2= districtListMapper.getCountyid(aa.getID());//第二等级
                for(DistrictList ab: ad2){
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictList> ad3= districtListMapper.getCountyid(ab.getID());//第三等级
                    for(DistrictList ac: ad3){
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictList> ad4= districtListMapper.getCountyid(ac.getID());//第四等级
                        for(DistrictList ad: ad4){
                            //将这一等级添加到lists
                            if(ad!=null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }
            List<DistrictList> list =null;
            int count = lists.size();//获取总数
            int start =page*limit-20;//开始数量
            int end =page*limit;//结束数量
            if(count>end){//总量 大于请求数量  用请求数量
                list = lists.subList( start, end);
            }else{
                list = lists.subList( start, count);
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    @Override
    public List<DistrictList> getAllDistrictss(int id) {
        try {
            //获取该管理员的地址
            List<DistrictList> lists=districtListMapper.getCountyById(id);
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictList> ad1= districtListMapper.getCountyid(lists.get(0).getID());//第一等级
            for(DistrictList aa: ad1 ){
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictList> ad2= districtListMapper.getCountyid(aa.getID());//第二等级
                for(DistrictList ab: ad2){
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictList> ad3= districtListMapper.getCountyid(ab.getID());//第三等级
                    for(DistrictList ac: ad3){
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictList> ad4= districtListMapper.getCountyid(ac.getID());//第四等级
                        for(DistrictList ad: ad4){
                            //将这一等级添加到lists
                            if(ad!=null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }
            return lists;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional
    public int checkDistrictName(String district_name,int id,String name) {
        int res = districtListMapper.checkDistrictName(district_name);
        if(res>0){//表示存在
            return 2;
        }
        DistrictList dis = new DistrictList();
        dis.setDISTRICT_NAME(district_name);
        dis.setPARENT_ID(id);
        dis.setPARENT_NAME(name);
        return districtListMapper.addDistrictName(dis);

    }
    @Override
    @Transactional
    public int checkDistrictNameAdmin(String district_name, String province, String city, Integer data_level,String server_id,int id,String name,String admin) {
        int res = districtListMapper.checkDistrictName(district_name);
        if (res > 0) {//表示存在
            return 2;
        }
        String school_id = "";
        String sql = "select school_id from [classificationofreading].[dbo].[b_school] where school_name = ? and type = ?";
        List<Map<String, Object>> schoolList = jdbcTemplate.queryForList(sql, district_name, 1);
        if (null != schoolList && schoolList.size() > 0) {
            school_id = schoolList.get(0).get("school_id") + "";
        }
        DistrictList dis = new DistrictList();
        dis.setDISTRICT_NAME(district_name);
        dis.setPARENT_ID(id);
        dis.setPARENT_NAME(name);
        dis.setSERVER_ID(server_id);
        dis.setADMIN(admin);
        dis.setPROVINCE(province);
        dis.setCITY(city);
        dis.setDATA_LEVEL(data_level);
        dis.setSCHOOL_ID(school_id);
        return districtListMapper.addDistrictNameAdmin(dis);

    }
    @Override
    @Transactional
    public int updateDistrict(Integer ID,String district_name,String province, String city, Integer data_level) {
        // int res = districtListMapper.checkDistrictName(district_name);
        // if(res>0){//表示存在
        //     return 2;
        // }
        List<DistrictList> lists=districtListMapper.getCountyid(ID);
        if (lists.size() > 0) {
            for (DistrictList ls : lists) {
                //更换下级单位的父辈名称
                districtListMapper.updateDistrictParentName(ls.getID(), district_name);
            }
        }
        String school_id = "";
        String sql = "select school_id from [classificationofreading].[dbo].[b_school] where school_name = ? and type = ?";
        List<Map<String, Object>> schoolList = jdbcTemplate.queryForList(sql, district_name, 1);
        if (null != schoolList && schoolList.size() > 0) {
            school_id = schoolList.get(0).get("school_id") + "";
        }
        return districtListMapper.updateDistrict(ID,district_name,province,city,data_level,school_id);
    }

    @Override
    @Transactional
    public int deleteDistrict(Integer id,Administrators ads) {
        //获取该管理员的地址
        List<DistrictList> lists=districtListMapper.getCountyById(id);
        //根据自己的等级获取下一等级的所有管理员
        List<DistrictList> ad1= districtListMapper.getCountyid(lists.get(0).getID());//第一等级
        for(DistrictList aa: ad1 ){
            //将这一等级添加到lists
            lists.add(aa);
            List<DistrictList> ad2= districtListMapper.getCountyid(aa.getID());//第二等级
            for(DistrictList ab: ad2){
                //将这一等级添加到lists
                lists.add(ab);
                List<DistrictList> ad3= districtListMapper.getCountyid(ab.getID());//第三等级
                for(DistrictList ac: ad3){
                    //将这一等级添加到lists
                    lists.add(ac);
                    List<DistrictList> ad4= districtListMapper.getCountyid(ac.getID());//第四等级
                    for(DistrictList ad: ad4){
                        //将这一等级添加到lists
                        if(ad!=null) {
                            lists.add(ad);
                        }
                    }
                }
            }
        }
        int res= 0;
        //改版新    将redis中的活动信息写入数据库
        // redisService.setActivityDetailFromRedisToSqlServer(ads.getLAST_ACTIVITY_ID());
        //改版新    将redis中的作品信息写入数据库
        // redisService.setWorkDetailFromRedisToSqlServer(ads.getLAST_ACTIVITY_ID());
        for(DistrictList ls:lists){//循环删除该地区及下属地区所有的地区及管理员
            Administrators lt = administratorsMapper.getAllAdminByDisid(ls.getID());//获取所有待删除的管理员
            //添加到管理员删除信息表中
            //获取本管理员的信息
            if(lt!=null) {
                AdminDelete adminDelete = new AdminDelete();
                adminDelete.setADMIN_ID(ads.getID());
                adminDelete.setADMIN_ACCOUNT(ads.getADMIN_ACCOUNT());
                adminDelete.setADMIN_NAME(ads.getADMIN_NAME());
                adminDelete.setADMIN_PHONE(ads.getPHONE());
                adminDelete.setBE_ADMIN_ID(lt.getID());
                adminDelete.setBE_ADMIN_ACCOUNT(lt.getADMIN_ACCOUNT());
                adminDelete.setBE_ADMIN_NAME(lt.getADMIN_NAME());
                adminDelete.setBE_ADMIN_PHONE(lt.getPHONE());
                //将该管理员中的活动删除
                activityMapper.deleteActivityByAdminAccount(lt.getADMIN_ACCOUNT());
                adminDeleteMapper.insertDeleteMesg(adminDelete);
                administratorsMapper.deleteAdminBydisId(ls.getID());
            }
            //将地区删除信息添加到表中
            DistrictDelete dis = new DistrictDelete();
            dis.setADMIN_ID(ads.getID());
            dis.setADMIN_ACCOUNT(ads.getADMIN_ACCOUNT());
            dis.setADMIN_NAME(ads.getADMIN_NAME());
            dis.setADMIN_PHONE(ads.getPHONE());
            dis.setDISTRICT_ID(ls.getID());
            dis.setDISTRICT_NAME(ls.getDISTRICT_NAME());
            int x = adminDeleteMapper.insertDeleteDistrictMsg(dis);
            res = districtListMapper.deleteDistrict(ls.getID());//删除地区
        }
        return res;
    }

    @Override
    @Transactional
    public int updateDisplay(Integer id, String display) {
        DistrictList dis = new DistrictList();
        dis.setID(id);
        if(display.equals("false")){//不显示
            dis.setIS_DISPLAY(1);
        }else{
            dis.setIS_DISPLAY(0);
        }
        return districtListMapper.updateDisplay(dis);
    }

    @Override
    public List<UserWorkVo> getListOne(String id) {
        int ID = Integer.valueOf(id);
        List<UserWorkVo> lists=workMapper.getWorkByActivityIdExportOne(ID);
        for(UserWorkVo uwv : lists){
            uwv.setDISTRICT_NAME(districtListMapper.getCountyName(uwv.getDISTRICT_ID()));
        }
        return lists;
    }

    @Override
    @Transactional
    public int deleteActivity(String activity_id) {
        return activityMapper.deleteActivity(activity_id);
    }

    @Override
    public UserWorkVo getWorkDetail(int work_id) {
        UserWorkVo work = workMapper.getWorkDetail(work_id);
        if("2".equals(work.getWORK_TYPE())){
            String[] URL = work.getRESOURCE_URL().split(",");//分割出来的字符数组
            List<Map<String ,String >> list = new ArrayList<>();
            for(int i = 0; i<URL.length;i++){
                Map<String,String> map =new HashMap<String, String>();
                map.put("url",URL[i]);
                list.add(map);
            }
            work.setURL(list);
        }
        return work;
    }

    @Override
    public Activity getNumber(int activity_id) {
        return activityMapper.getNumber(activity_id);
    }

    @Override
    @Transactional
    public int setPrize(int activity_id, int i, Integer integer) {
        int res = 0 ;
//        if(i==1){
//            res = activityMapper.setPrize1(activity_id,integer);
//        }else if(i==2){
//            res = activityMapper.setPrize2(activity_id,integer);
//        }else if(i==3){
//            res = activityMapper.setPrize3(activity_id,integer);
//        }
        return res;
    }

    @Override
    @Transactional
    public int setACTIVITY_WIN(int activity_id, int activity_win) {
        return activityMapper.setACTIVITY_WIN(activity_id,activity_win);
    }

    @Override
    public int setSubmit(int is_submit, int activity_id) {
        return activityMapper.setSubmit(is_submit, activity_id);
    }


    @Override
    @Transactional
    public int deletePrize(Integer PrizeId) {
        int res = prizeMapper.deletePrizeByPrizeId(PrizeId);
        if(res == 1){//删除成功！   把作品表中的奖项删除掉
            workMapper.setWorkPrizeDelete(PrizeId);
        }
        return res;
    }

    @Override
    @Transactional
    public int backUpdateWork(UserWorkVo work) {
        int res = 0;
        //首先根据work_id找出用户id
        String user_id = userMapper.getUserId(work.getWORK_ID());
        //根据user_id设置用户信息
        work.setUSER_ID(user_id);
        res = userMapper.updateUserRegInfo(work);
        //根据work_id设置作品信息
        if(work.getRESOURCE_URL()==null || "".equals(work.getRESOURCE_URL())){//（一种是光修改作品名称）
            res = workMapper.updateWorkName(work);
        }else{//（一种是全部修改）
            res = workMapper.updateWork(work);
        }
        return res;
    }

    @Override
    @Transactional
    public int updateTime(Activity activity) {
        return activityMapper.updateTime(activity);
    }

    @Override
    @Transactional
    public int setEntryTime(Activity activity) {
        return activityMapper.setEntryTime(activity);
    }

    @Override
    public PageInfo<Article> getReadWork(int page, int limit, String name) {
        try {
            //开启分页
            PageHelper.startPage(page,limit);
            List<Article> lists=workMapper.getReadWorks(name);
            PageInfo<Article> list= new PageInfo<>(lists);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional
    public int deleteArticle(Integer id) {
        return workMapper.deleteArticle(id);
    }

    @Override
    @Transactional
    public int updateArticle(Article article,MpInfo mp) {
        //根据serverId获取路径
        String path = FileUtils.saveHtml(article.getARTICLE_URL(),Utils.getName()+".html",mp);
        article.setARTICLE_URL(path);
        return workMapper.updateArticle(article);
    }

    @Override
    @Transactional
    public int addArticle(Article article,MpInfo mp) {
        String path = FileUtils.saveHtml(article.getARTICLE_URL(),Utils.getName()+".html",mp);
        article.setARTICLE_URL(path);
        return workMapper.addArticle(article);
    }

    @Override
    public Article getArticle(Integer id) {
        return workMapper.getArticle(id);
    }

    @Override
    public Administrators getAdminByAccount(String admin_account) {
        return administratorsMapper.getAdminByAccount(admin_account);
    }

    @Override
    public String getLastMpInFoServerId() {
        return String.valueOf(Integer.parseInt(mpInFoMapper.getLastMpInFo())+1);
    }

    @Override
    public String getServerId(Integer district_id) {
        return districtListMapper.getServerId(district_id);
    }

    @Override
    public Logo getLogo(Administrators ad) {
        //首先获取服务器的logo
        //Logo logo= logoMapper.getLogo(ad.getSERVER_ID());
        Logo logo=logoMapper.getLogos(ad.getADMIN_ACCOUNT());;
        if(logo==null){
            logo = logoMapper.getLogos("feiyuebook");
        }
        //获取管理员头像
        String face_img = logoMapper.getAdminFace(ad.getID());
        if ("".equals(face_img)||null==face_img) {
            logo.setFACE_IMG(logo.getFANCE_IMG());
        }else{
            logo.setFACE_IMG(face_img);
        }

        return logo;
    }

    @Override
    public String getIsSubmit(int last_activity_id) {
        ActivityPrizeVo ac = activityMapper.getActivityDetails(last_activity_id);
        return ac.getIS_SUBMIT();
    }

    @Override
    public int setWINNERS(int winners, int activity_id) {
        return activityMapper.setWINNERS(winners, activity_id);
    }

    @Override
    public List<Prize> getPrizeListByActivityId(int activity_id) {
        //根据活动id获取该活动中的奖项信息
        return prizeMapper.getPrizeListByActivityId(activity_id);
    }

    @Override
    public List<GroupRule> getGroupRuleListByActivityId(int activity_id) {
        return groupRuleMapper.getGroupRuleListByActivityId(activity_id);
    }

    @Override
    public boolean islosePrize(Prize prize,int ACTIVITY_ID) {
        //获取该奖项的人数
        Prize p = prizeMapper.getPrizeByid(prize.getPRIZE_ID());
        //获取该活动下获得该奖项的人数
        int res = prizeMapper.getCountWork(prize.getPRIZE_ID(),ACTIVITY_ID);
        if(p.getPRIZE_USERNUMBER()==res){//不能添加
            return false;
        }else{
            return true;
        }

    }

    @Override
    public int setWorkPrize(Prize prize) {
        return workMapper.setWorkPrize(prize);
    }

    @Override
    public int delWorkPrize(Prize prize) {
        return workMapper.delWorkPrize(prize);
    }


    @Override
    public int updateActivityRules(Activity activity) {
        return activityMapper.updateActivityRules(activity);
    }

    @Override
    public int setIsRote(int is_rote, int activity_id) {
        return activityMapper.setIsRote(is_rote, activity_id);
    }

    @Override
    public PageInfo<Probation> getAllProbation(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Probation> lists = logoMapper.getAllProbation();
        PageInfo<Probation> list= new PageInfo<>(lists);
        return list;
    }

    @Override
    public int updateProbtation(Probation probation) {
        return logoMapper.updateProbtation(probation);
    }

    @Override
    public Probation getProbation(Administrators login) {
        return logoMapper.getProbation(login);
    }


    @Override
    public int award(int work_id) {
        return workMapper.award(work_id);
    }

    @Override
    public PrizeUser getPrizeUser(int activity_id) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PrizeUser prizeUser = workMapper.getPrizeUser(activity_id);
        if(prizeUser!=null){
            prizeUser.setSTART(formatter.format(prizeUser.getSTART_TIME()));
            prizeUser.setEND(formatter.format(prizeUser.getEND_TIME()));
        }
        return prizeUser;
    }

    @Override
    public int setPrizeUser(PrizeUser prizeUser) {
        PrizeUser p = workMapper.getPrizeUser(prizeUser.getACTIVITY_ID());
        int res = 0;
        if (p == null) {//添加
            res = workMapper.insertPrizeUser(prizeUser);
        } else {
            res = workMapper.updatePrizeUser(prizeUser);
        }
        return res;
    }

    private void getChildrenIds(Integer districtId, List<Integer> districtIdList) {
        // 递归获取该地区下的所有地区
        List<DistrictList> list = districtListMapper.getCountyid(districtId);
        if (null != list && list.size() > 0) {
            for (DistrictList dl : list) {
                districtIdList.add(dl.getID());
                getChildrenIds(dl.getID(), districtIdList);
            }
        }
    }
}






















